package cc.admin.modules.graduate.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.aspect.annotation.AutoLog;
import cc.admin.common.sys.base.controller.BaseController;
import cc.admin.common.sys.query.QueryGenerator;
import cc.admin.common.sys.vo.LoginUser;
import cc.admin.modules.graduate.entity.BlankQuestions;
import cc.admin.modules.graduate.entity.ChoiceQuestions;
import cc.admin.modules.graduate.entity.ExamPaperAnswer;
import cc.admin.modules.graduate.entity.VO.ExamPaperVo;
import cc.admin.modules.graduate.service.IExamPaperAnswerService;
import cc.admin.modules.sys.entity.SysCascade;
import cc.admin.modules.sys.service.ISysCascadeService;
import cc.admin.modules.graduate.entity.ExamPaper;
import cc.admin.modules.graduate.service.IExamPaperService;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 试卷
 * @Author: cc-admin
 * @Date:   2024-12-25
 * @Version: V1.0.0
 */
@Slf4j
@Api(tags="试卷")
@RestController
@RequestMapping("/exam/paper")
public class ExamPaperController extends BaseController<ExamPaper, IExamPaperService> {
	@Autowired
	private IExamPaperService examPaperService;

	@Autowired
	private IExamPaperAnswerService examPaperAnswerService;

	/**
	 * 分页列表查询
	 *
	 * @param key
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "试卷-分页列表查询")
	@ApiOperation(value="试卷-分页列表查询", notes="试卷-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(
								   @RequestParam(name="key",required = false) String key,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ExamPaper> queryWrapper = QueryGenerator.initQueryWrapper(new ExamPaper(), req.getParameterMap());
		if (StrUtil.isNotEmpty(key)) {
            
		}
		Map<String, String[]> parameterMap = req.getParameterMap();
		String[] catalogs = parameterMap.get("catalog");
		String tab = catalogs[0];
		queryWrapper.eq("subject_id",tab);
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		queryWrapper.eq("create_by",user.getUsername());
		queryWrapper.and(wrapper -> wrapper.isNull("type").or().ne("type", "已完成"));		Page<ExamPaper> page = new Page<ExamPaper>(pageNo, pageSize);
		IPage<ExamPaper> pageList = examPaperService.page(page, queryWrapper);
//		pageList.getRecords().forEach(examPaper -> {
//			List<String> cChoiceIdsollect = Arrays.stream(examPaper.getChoiceIds().split(",")).collect(Collectors.toList());
//			examPaper.setPercent(collect);
//		});
		return Result.ok(pageList);
	}

	/**
	 * 添加
	 *
	 * @param examPaper
	 * @return
	 */
	@AutoLog(value = "试卷-添加")
	@ApiOperation(value="试卷-添加", notes="试卷-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ExamPaper examPaper) {
        examPaper.setId(IdUtil.objectId());
		HashMap<String, List<ChoiceQuestions>> exam = examPaperService.createExam(examPaper);
//        examPaperService.save(examPaper);
		return Result.ok(exam);
	}

	/**
	 * 编辑
	 *
	 * @param examPaper
	 * @return
	 */
	@AutoLog(value = "试卷-编辑")
	@ApiOperation(value="试卷-编辑", notes="试卷-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ExamPaper examPaper) {
        examPaperService.updateExam(examPaper);
//        examPaperService.updateById(examPaper);
		return Result.ok("编辑成功!");
	}


	/**
	 *   复制
	 * @param id
	 * @param name
	 * @return
	 */
	@AutoLog(value = "试卷-复制")
	@ApiOperation(value="试卷-复制", notes="试卷-复制")
	@PostMapping(value = "/copy")
	public Result<?> copy(@RequestParam(name="id",required=true) String id,@RequestParam(name="name",required=true) String name) {
        ExamPaper examPaper = examPaperService.getById(id);
        examPaper.setId(IdUtil.fastUUID());
        examPaper.setCreateTime(new Date());
        examPaperService.save(examPaper);
		return Result.ok("复制成功！");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "试卷-通过id删除")
	@ApiOperation(value="试卷-通过id删除", notes="试卷-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        examPaperService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "试卷-批量删除")
	@ApiOperation(value="试卷-批量删除", notes="试卷-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.examPaperService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "试卷-通过id查询")
	@ApiOperation(value="试卷-通过id查询", notes="试卷-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        ExamPaper examPaper = examPaperService.getById(id);
		return Result.ok(examPaper);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param examPaper
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, ExamPaper examPaper) {
      return super.exportXls(request, examPaper, ExamPaper.class, "试卷");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, ExamPaper.class);
  }

  @PostMapping("/getexampaper")
  public Result getExamPaper(@RequestBody  String id){
	log.info("试卷id:{}",id);
	ExamPaperVo examPaperVo = examPaperService.getexampaper(id);
	return Result.ok(examPaperVo);
  }

	@PostMapping("/examAdd")
	public Result examAddExamPaper(@RequestBody ExamPaperAnswer examPaperAnswer){
		log.info("试卷:{}",examPaperAnswer);
		examPaperService.saveExamAnswer(examPaperAnswer);
		return Result.ok();
	}

	/**
	 * 分页列表查询
	 *
	 * @param key
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "试卷-分页列表查询")
	@ApiOperation(value="试卷-分页列表查询", notes="试卷-分页列表查询")
	@GetMapping(value = "/viewList")
	public Result<?> queryPaperList(
			@RequestParam(name="key",required = false) String key,
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			HttpServletRequest req) {
		QueryWrapper<ExamPaperAnswer> queryWrapper = QueryGenerator.initQueryWrapper(new ExamPaperAnswer(), req.getParameterMap());
		if (StrUtil.isNotEmpty(key)) {

		}
		Page<ExamPaperAnswer> page = new Page<ExamPaperAnswer>(pageNo, pageSize);
		Map<String, String[]> parameterMap = req.getParameterMap();
		String[] catalogs = parameterMap.get("catalog");
		String tab = catalogs[0];
		queryWrapper.eq("subject_id",tab);
		queryWrapper.eq("type","已完成");
		IPage<ExamPaperAnswer> pageList = examPaperAnswerService.page(page, queryWrapper);
		List<ExamPaperAnswer> records = pageList.getRecords();
		examPaperAnswerService.getPaper(records);
		return Result.ok(pageList);
	}

	/**
	 * 分页列表查询
	 *
	 * @param key
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "试卷-分页列表查询")
	@ApiOperation(value="试卷-分页列表查询", notes="试卷-分页列表查询")
	@GetMapping(value = "/viewList2")
	public Result<?> queryUserPaperList(
			@RequestParam(name="key",required = false) String key,
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			HttpServletRequest req) {
		QueryWrapper<ExamPaperAnswer> queryWrapper = QueryGenerator.initQueryWrapper(new ExamPaperAnswer(), req.getParameterMap());
		if (StrUtil.isNotEmpty(key)) {

		}
		Page<ExamPaperAnswer> page = new Page<ExamPaperAnswer>(pageNo, pageSize);
		Map<String, String[]> parameterMap = req.getParameterMap();
		String[] catalogs = parameterMap.get("catalog");
		String tab = catalogs[0];
		queryWrapper.eq("subject_id",tab);
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		queryWrapper.eq("create_by",user.getUsername());
		queryWrapper.eq("type","已审批");
		IPage<ExamPaperAnswer> pageList = examPaperAnswerService.page(page, queryWrapper);
		List<ExamPaperAnswer> records = pageList.getRecords();
		examPaperAnswerService.getPaper(records);
		return Result.ok(pageList);
	}

}
